package com.snut.tomorrowNews.controller.front;

import com.snut.tomorrowNews.common.CommonResult;
import com.snut.tomorrowNews.model.*;
import com.snut.tomorrowNews.service.front.WegmsgService;
import com.snut.tomorrowNews.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(path = "/webmsg")
public class WebmsgController {
    @Autowired
    WegmsgService webmsgService;
    @GetMapping(path = "/webmsg")
    public CommonResult findadmin(HttpSession session){
        CommonResult commonResult = null;
        try {
            Webmsg webmsg1=webmsgService.select();
            session.setAttribute("webmsg",webmsg1);
            commonResult = new CommonResult(200, "查询成功", webmsg1);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
//上传logo
    @PostMapping(path = "/logo")
    public CommonResult upload(@RequestParam("filename") CommonsMultipartFile file, HttpSession session){
        CommonResult commonResult =null;
        System.out.println("ssddsd");
        String FileName=file.getOriginalFilename();
        Webmsg webmsg1 = (Webmsg)session.getAttribute("webmsg");
        String newFileName= StringUtil.getNewFileName(FileName);
        Webmsg webmsg=new Webmsg();
        webmsg.setLogoName(newFileName);
        webmsg.setId(webmsg1.getId());
        System.out.println(newFileName);
        File f0 = new File("D:\\test\\tomorrowNewsFiles\\logo");
        if(!f0.exists()){
            f0.mkdirs();
        }
        File f = new File(f0, newFileName);//输出路径
        try {
            file.transferTo(f);
            webmsgService.uploadlogo(webmsg);
            commonResult =new CommonResult(200,"上传成功",webmsg);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult =new CommonResult(500,"登录失败",null);
        }
        return commonResult;
    }
//新闻类型
    @GetMapping(path = "/newstype")
    public CommonResult newstype(){
        CommonResult commonResult = null;
        try {
            List<Webmsg> webmsg1=webmsgService.newtypeselect();
            commonResult = new CommonResult(200, "查询成功", webmsg1);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }



    //友情链接
    @GetMapping(path = "/links")
    public CommonResult links(){
        CommonResult commonResult = null;
        try {
            List<Webmsg> webmsg1=webmsgService.linksselect();
            commonResult = new CommonResult(200, "查询成功", webmsg1);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    //修改友情链接显示
    @GetMapping(path = "/findlink")
    public CommonResult findlink(Integer id){
        CommonResult commonResult = null;
        try {
            Webmsg webmsg=webmsgService.findlink(id);
            commonResult = new CommonResult(200, "查询成功", webmsg);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //修改保存
    @PostMapping(path = "/savelink")
    public CommonResult savelink(Webmsg webmsg){
        CommonResult commonResult = null;
        try {
            webmsgService.savelink(webmsg);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //添加保村
    @PostMapping(path = "/save1link")
    public CommonResult save1link(Webmsg webmsg,HttpSession session){
        CommonResult commonResult = null;
        try {
            Admin admin = (Admin) session.getAttribute("admin");
            webmsg.setOperater(admin.getId());
            webmsgService.save1link(webmsg);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //友情链接
    @GetMapping(path = "/deletelink")
    public CommonResult deletelink(Integer id){
        CommonResult commonResult = null;
        try {
            webmsgService.deletelink(id);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    //根据类型排序
    @GetMapping(path = "/newstypeid")
    public CommonResult newstypeid(Integer id){
        CommonResult commonResult = null;
        try {
            List<News> news=webmsgService.newtype(id);
            commonResult = new CommonResult(200, "查询成功", news);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
//最新推荐
    @GetMapping(path = "/newnewstypeid")
    public CommonResult newnewstypeid(){
        CommonResult commonResult = null;
        try {
            List<News> news=webmsgService.newnewstype();
            commonResult = new CommonResult(200, "查询成功", news);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //搜索
    @GetMapping(path = "/search")
    public CommonResult serach(String searchname){
        CommonResult commonResult = null;
        try {
            String str=searchname;
            str="["+str+"]";
            System.out.println(str);
            List<News> searchnew=webmsgService.search(str);
            commonResult = new CommonResult(200, "查询成功", searchnew);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //广告位显示
    @GetMapping(path = "/admsg")
    public CommonResult admsg(){
        CommonResult commonResult = null;
        try {
            List<Admsg> admsgs=webmsgService.admsg();
            commonResult = new CommonResult(200, "查询成功", admsgs);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    //删除广告位显示
    @GetMapping(path = "/deleteadmsg")
    public CommonResult deleteadmsg(Integer id){
        CommonResult commonResult = null;
        try {
            webmsgService.deleteadmsg(id);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //修改广告位显示
    @GetMapping(path = "/findadmsg")
    public CommonResult findadmsg(Integer id){
        CommonResult commonResult = null;
        try {
            Admsg admsg=webmsgService.findadmsg(id);
            commonResult = new CommonResult(200, "查询成功", admsg);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    //根据跳转页面查询某个新闻内容
    @GetMapping(path = "/newid")
    public CommonResult newid(Integer id){
        CommonResult commonResult = null;
        try {
            News webmsg1=webmsgService.newidselect(id);
            commonResult = new CommonResult(200, "查询成功", webmsg1);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    @PostMapping(path = "/collect")
    public CommonResult collect(Integer userId,Integer Id){
        CommonResult commonResult = null;
        try {
            System.out.println(userId);
            System.out.println(Id);
            webmsgService.collect(userId,Id);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    //推荐文章
    @GetMapping(path = "/recommend")
    public CommonResult recommend(){
        CommonResult commonResult = null;
        try {
            List<News> admsgs=webmsgService.recommend();
            commonResult = new CommonResult(200, "查询成功", admsgs);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    //点击最多
    @GetMapping(path = "/click")
    public CommonResult click(){
        CommonResult commonResult = null;
        try {
            List<News> admsgs=webmsgService.click();
            commonResult = new CommonResult(200, "查询成功", admsgs);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    //增加点击量
    @PostMapping(path = "/clicknum")
    public CommonResult clicknum(Integer aid,Integer rid){
        CommonResult commonResult = null;
        try {
            System.out.println(aid);
            System.out.println(rid);


            webmsgService.clicknum(aid,rid);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
//修改后上传
    @PostMapping(path = "/picture")
    public CommonResult updateadmsg(@RequestParam("filename") CommonsMultipartFile file, Integer id){
        CommonResult commonResult =null;
        System.out.println("ssddsd");
        System.out.println(file);
        String FileName=file.getOriginalFilename();
        String newFileName= StringUtil.getNewFileName(FileName);
        Admin admin1=new Admin();
        admin1.setId(id);
        admin1.setNewFileName(newFileName);
        System.out.println(newFileName);
        File f0 = new File("D:\\test\\tomorrowNewsFiles\\ad");
        if(!f0.exists()){
            f0.mkdirs();
        }
        File f = new File(f0, newFileName);//输出路径
        try {
            file.transferTo(f);
            webmsgService.uploadadmsg(admin1);
            commonResult =new CommonResult(200,"上传成功",null);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult =new CommonResult(500,"登录失败",null);
        }
        return commonResult;
    }
    //添加
    @PostMapping(path = "/picturme")
    public CommonResult updateadmsg1(@RequestParam("filename") CommonsMultipartFile file,Admsg admsg){
        CommonResult commonResult =null;
        System.out.println("ssddsd");
        System.out.println(admsg.getName());
        System.out.println(admsg.getPosition());
        System.out.println(admsg.getUrl());
        File f0 = new File("D:\\test\\tomorrowNewsFiles\\ad");
     /*   if(!f0.exists()){
            f0.mkdirs();
        }*/
        File f = new File(f0, admsg.getName());//输出路径
        try {
            file.transferTo(f);
            commonResult =new CommonResult(200,"上传成功",null);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult =new CommonResult(500,"登录失败",null);
        }
        return commonResult;
    }

    //新闻类型修改查询
    @GetMapping(path = "/findnewstype")
    public CommonResult findnewstype(Integer id){
        CommonResult commonResult = null;
        try {
            News news=webmsgService.findnewstype(id);
            commonResult = new CommonResult(200, "查询成功", news);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
//删除某个新闻类型
    @GetMapping(path = "/deletenewstype")
    public CommonResult deletenewstype(Integer id){
        CommonResult commonResult = null;
        try {
            webmsgService.deletenewstype(id);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //修改某个新闻类型
    @PostMapping(path = "/savenewstype")
    public CommonResult savenewstype(News news){
        CommonResult commonResult = null;
        try {
            webmsgService.savenewstype(news);

            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //添加
    @PostMapping(path = "/save1newstype")
    public CommonResult save1newstype(NewsType newsType, HttpSession session){
        CommonResult commonResult = null;
        try {

            Admin admin = (Admin) session.getAttribute("admin");
            newsType.setOperaterId(admin.getId());
            webmsgService.save1newstype(newsType);
            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }
    //修改网页信息
    @PostMapping(path = "/savewebmsg")
    public CommonResult savewebmsg(Webmsg webmsg,HttpSession session){
        CommonResult commonResult = null;
        try {
            Admin admin = (Admin) session.getAttribute("admin");
            webmsg.setOperater(admin.getId());
            webmsgService.savewebmsg(webmsg);

            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

    @PostMapping(path = "/saveadmsg")
    public CommonResult saveadmsg(Admsg admsg){
        CommonResult commonResult = null;
        try {

            webmsgService.saveadmsg(admsg);

            commonResult = new CommonResult(200, "查询成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败",null);
        }
        return commonResult;
    }

}
